<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Send;
use think\Config;
use think\Request;
use think\Db;
 

/**
 * 示例接口1
 */
class Shop extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
	

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
	 public function _initialize()
    {
        parent::_initialize();
    }
	//基础权限验证
	public function check()
    {
      
		$uid=input('uid');
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数有误";
			return $data;
		}
		$user=db('user')->find($uid);
		if(empty($user)){
			$data['sta']=0;
			$data['msg']="用户信息有误";
			return $data;
		}
		$shop=db('shop')->find($shop_id);
		
		if(empty($shop)){
			$data['sta']=0;
			$data['msg']="店铺信息有误";
			return $data;
		}
		$admins=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->find();
		if(empty($admins)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return $data;
		}else{
			$data['sta']=1;
			$data['shop']=$shop;
			$data['uid']=$uid;
			$data['shop_id']=$shop_id;
			return $data;
		}
    }
	//店铺状态修改  打烊、营业
	public function edit_stattus(){
		$shop=$this->check();
		if($shop['sta']==0){
		  $data['sta']=0;
		  $data['sta']=$shop['msg'];
		  return json($data);
		}
		$ashop=$shop['shop'];
		$ashop['status']=(int)!$ashop['status'];
		 
		Db::startTrans();
		try{
			db('shop')->update($ashop);
			Db::commit();
			$data['sta']=1;
			$data['msg']=$ashop;
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}	
	//优惠券添加
    public function card_add(){
		$shop=$this->check();
		if($shop['sta']==0){
		  $data['sta']=0;
		  $data['sta']=$shop['msg'];
		  return json($data);
		}
		$card['shop_id']=$shop['shop_id'];
		$card['user_id']=$shop['uid'];
		$card['name']=input('name');
		if(empty($card['name'])){
			$data['sta']=0;
			$data['msg']="请输入优惠券标题";
			return json($data);
		}
		$card['man']=input('man');
		
		$card['jian']=input('jian');
		if(empty($card['man'])||empty($card['jian'])){
			$data['sta']=0;
			$data['msg']="请设置优惠券条件";
			return json($data);
		}
		$card['endtime']=input('endtime');
		if(empty($card['endtime'])){
			$data['sta']=0;
			$data['msg']="请输入过期时间";
			return json($data);
		}
		$card['createtime']=time();
		Db::startTrans();
		try{
			db('card')->insertGetId($card);
			Db::commit();
			$data['sta']=1;
			$data['msg']="创建成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	/*public function qrcode(){
		vendor("phpqrcode.phpqrcode");
		$b=new  \QRcode();
		$uid=(int)input('uid');
		if($uid==0){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		$url="{'uid':".$uid.",'time':".time()."}";
		$level="L";
		$size=3;
		$margin=2;
		ob_start();
		$a=$b::png($url,false,$level,$size,$margin);
		$imageString = base64_encode(ob_get_contents());
		ob_end_clean();
		echo  "<img src='data:image/jpg;base64,".$imageString."'>";
		 
	}*/
	//获取店铺信息
	public function get_shop(){
		$shop=$this->check();
		$shop['admin']=db('ShopAdmin')->where('user_id',$shop['uid'])->where('shop_id',$shop['shop_id'])->select();
		$shop['admin_arr']=array();
		foreach($shop['admin'] as $k=>$v){
			$shop['admin_arr'][]=$v['is_admin'];
		}
		return json($shop);
	}
	//优惠券列表
	public function card_list(){
	  $shop=$this->check();
	   if($shop['sta']==0){
		  $data['sta']=0;
		  $data['msg']=$shop['msg'];
		  return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$status=input('status')?input('status'):1;
		$list=db('card')->where('shop_id',$shop['shop_id'])->where('status',$status)->limit($start,$pagesize)->order("createtime","desc")->select();
		 if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		 }else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		 }
	}
	//下架优惠券
	public function card_stop(){
		$card_id=(int)input('card_id');
		$uid=(int)input('uid');
		if($uid==0||$card_id==0){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		$card=db('card')->find($card_id);
		if(empty($card)){
			$data['sta']=0;
			$data['msg']="优惠券不存在";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$card['shop_id'])->where('user_id',$uid)->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		if($card['status']==1){
			$card['status']=0;
			Db::startTrans();
			try{
				db('card')->update($card);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		}else{
			$data['sta']=0;
			$data['msg']="状态有误";
			return json($data);
		}
	}
	//获取用户权限  扫码返回
	public function get_userinfo(){
		$uid=(int)input('uid');
		$shop_id=(int)input('shop_id');
		$xuid=(int)input('xuid');
		if($uid==0||$shop_id==0||$xuid==0){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		$disadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->where('is_admin',1)->find();
		if(empty($disadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
			$userinfo=db('user')->field('id,nickname,avatar')->find($xuid);
			$isadmin=db('ShopAdmin')->where('user_id',$xuid)->where('shop_id',$shop_id)->select();
			$adminarr=array();
			foreach($isadmin as $k=>$v){
				$adminarr[]=$v['is_admin'];
			}
			$data['sta']=1;
			$data['msg']=$userinfo;
			$data['isadmin']=$adminarr;
			return json($data);
	}
	//管理员添加
	public function admin_add(){
		$uid=(int)input('uid');
		$shop_id=(int)input('shop_id');
		$disadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->where('is_admin',1)->find();
		if(empty($disadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$xuid=(int)input('xuid');
		if($uid==0||$shop_id==0||$xuid==0){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		if($xuid==$uid){
			$data['sta']=0;
			$data['msg']="当前用户已是超级管理员";
			return json($data);
		}
		$is_admin=(int)input('is_admin');
		if($is_admin!=2&&$is_admin!=3){
			$data['sta']=0;
			$data['msg']="设置参数有误";
			return json($data);
		}
		$isadmin=db('ShopAdmin')->where('user_id',$xuid)->where('shop_id',$shop_id)->where('is_admin',$is_admin)->find();
		if(empty($isadmin)){
			$admin['user_id']=$xuid;
			$admin['shop_id']=$shop_id;
			$admin['is_admin']=$is_admin;
			Db::startTrans();
			try{
				db('ShopAdmin')->insertGetId($admin);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		}else{
			Db::startTrans();
			try{
				db('ShopAdmin')->delete($isadmin['id']);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		}
			 
	}
	//管理员列表
	public function admin_list(){
		$uid=(int)input('uid');
		$shop_id=(int)input('shop_id');
		$disadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->where('is_admin',1)->find();
		if(empty($disadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$adminlist=db('ShopAdmin')->field('user.nickname,user.avatar,user.id as uid,horse_shop_admin.*')->join('user','user.id=horse_shop_admin.user_id')->where('horse_shop_admin.shop_id',$shop_id)->where('horse_shop_admin.is_admin',2)->select();
		$jllist=db('ShopAdmin')->field('user.nickname,user.avatar,user.id as uid,horse_shop_admin.*')->join('user','user.id=horse_shop_admin.user_id')->where('horse_shop_admin.shop_id',$shop_id)->where('horse_shop_admin.is_admin',3)->select();
		$data['adminlist']=$adminlist;
		$data['jllist']=$jllist;
		return json($data);
	}
	//删除管理员
	public function admin_del(){
		$uid=(int)input('uid');
		$admin_id=(int)input('admin_id');
		if(empty($uid)||empty($admin_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->find($admin_id);
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="数据有误";
			return json($data);
		}
		$dadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$admin['shop_id'])->where('is_admin',1)->find();
		if(empty($dadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		Db::startTrans();
		try{
			db('ShopAdmin')->delete($admin_id);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
		
	}
	
	//获取是否为会员
	public function get_shopvip(){
		$uid=(int)input('uid');
		$shop_id=(int)input('shop_id');
		$disadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->where('is_admin','in',array(1,2))->find();
		if(empty($disadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$xuid=(int)input('xuid');
		$userinfo=db('user')->field('id,nickname,avatar')->find($xuid);
		$isadmin=db('ShopVip')->where('user_id',$xuid)->where('shop_id',$shop_id)->where('status',1)->find();
		$data['sta']=1;
		$data['msg']=$userinfo;
		$data['isadmin']=$isadmin;
		return json($data);
	}
	//添加会员
	public function add_vip(){
		$uid=(int)input('uid');
		$shop_id=(int)input('shop_id');
		$disadmin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$shop_id)->where('is_admin','in',array(1,2,3))->find();
		if(empty($disadmin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$xuid=(int)input('xuid');
		 
		$isadmin=db('ShopVip')->where('user_id',$xuid)->where('shop_id',$shop_id)->where('status',1)->find();
		if(empty($isadmin)){
			$vip['user_id']=$xuid;
			$vip['shop_id']=$shop_id;
			$vip['status']=1;
			$vip['createtime']=time();
			$vip['endtime']=time()+31536000;
			Db::startTrans();
			try{
				db('ShopVip')->insertGetId($vip);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				$data['isadmin']=$vip;
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
			
		}else{
			$isadmin['status']=0;
			Db::startTrans();
			try{
				db('ShopVip')->delete($isadmin['id']);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		}
	}
	//会员列表
	public function vip_list(){
	   $shop=$this->check();
	   if($shop['sta']==0){
		  $data['sta']=0;
		  $data['msg']=$shop['msg'];
		  return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$status=input('status')?input('status'):1;
		$list=db('ShopVip')->field('user.nickname,user.avatar,user.id as uid,horse_shop_vip.*')->join('user','user.id=horse_shop_vip.user_id')->where('shop_id',$shop['shop_id'])->where('horse_shop_vip.status',$status)->limit($start,$pagesize)->select();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	//课程添加
	public function class_add(){
		$shop=$this->check();
	    if($shop['sta']==0){
		  $data['sta']=0;
		  $data['msg']=$shop['msg'];
		  return json($data);
		}
		$name=input('name');
		if(empty($name)){
			$data['sta']=0;
			$data['msg']="请输入课程名称";
			return json($data);
		}
		$price=input('price');
		if(empty($price)){
			$data['sta']=0;
			$data['msg']="请输入课程价格";
			return json($data);
		}
		$class_num=input('class_num');
		if(empty($class_num)){
			$data['sta']=0;
			$data['msg']="请输入课时数";
			return json($data);
		}
		$images=input('images');
		if(empty($images)){
			$data['sta']=0;
			$data['msg']="请上传图片";
			return json($data);
		}
		
		$class['name']=$name;
		$class['price']=$price;
		$class['images']=$images;
		$class['class_num']=$class_num;
		$class['createtime']=time();
		$class['user_id']=$shop['uid'];
		$class['shop_id']=$shop['shop_id'];
		Db::startTrans();
		try{
			db('Class')->insertGetId($class);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//课程列表
	public function class_list(){
		$shop=$this->check();
	    if($shop['sta']==0){
		  $data['sta']=0;
		  $data['msg']=$shop['msg'];
		  return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$status=input('status')?input('status'):1;
		$list=db('class')->where('status',$status)->where('shop_id',$shop['shop_id'])->limit($start,$pagesize)->select();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	//下架课程
	public function class_stop(){
		$class_id=(int)input('class_id');
		$uid=(int)input('uid');
		if($uid==0||$class_id==0){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		$class=db('class')->find($class_id);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程不存在";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$class['shop_id'])->where('user_id',$uid)->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		if($class['status']==1){
			$class['status']=0;
			Db::startTrans();
			try{
				db('class')->update($class);
				Db::commit();
				$data['sta']=1;
				$data['msg']="操作成功";
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		}else{
			$data['sta']=0;
			$data['msg']="状态有误";
			return json($data);
		}
	}
	//时段添加
	public function time_add(){
		$uid=input('uid');
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$shop_id)->where('is_admin','in',array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$starttime=input('starttime');
		$endtime=input('endtime');
		if(empty($starttime)||empty($endtime)){
			$data['sta']=0;
			$data['msg']="请设置时间 ";
			return json($data);
		}
	 
		if((int)$starttime>(int)$endtime){
			$data['sta']=0;
			$data['msg']="请设置正确的时间";
			return json($data);
		}
		
		$map['shop_id']=$shop_id;
		$map1['starttime|endtime']=array('between',array($starttime,$endtime));
		$atime=db('ClassTime')->where($map)->where($map1)->find();
		
		if(!empty($atime)){
			$data['sta']=0;
			$data['msg']="与已有开课时间重合";
			return json($data);
		}
		$times['user_id']=$uid;
		$times['shop_id']=$shop_id;
		$times['starttime']=$starttime;
		$times['endtime']=$endtime;
		Db::startTrans();
		try{
			db('ClassTime')->insertGetId($times);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//时段列表
	public function time_list(){
		$uid=input('uid');
	 
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$shop_id)->where('is_admin','in',array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$list=db('ClassTime')->where('shop_id',$shop_id)->select();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	//删除时段
	public function time_del(){
		$uid=input('uid');
		if(empty($uid)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$time_id=input('time_id');
		$time=db('ClassTime')->find($time_id);
		if(empty($time)){
			$data['sta']=0;
			$data['msg']="时段不存在";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$time['shop_id'])->where('user_id',$uid)->where('is_admin','in',array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		Db::startTrans();
		try{
			db('ClassTime')->delete($time_id);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	
	//课程详情
	public function class_detail(){
	   $class_id=input('id');
	   $uid=input('uid');
	   if(empty($class_id)){
		    $data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
	   }
	   $detail=db('class')->find($class_id);
	   if(empty($detail)){
		   $data['sta']=0;
			$data['msg']="课程不存在";
			return json($data);
	   }
	   $shop=db('shop')->find($detail['shop_id']);
	   $class_cash=array();
	   if(!empty($uid)){
		    $class_cash=db('ClassCash')->where('user_id',$uid)->where('class_id',$class_id)->where('num','gt',0)->find();
				$point['class_id']=$class_id;
				$point['user_id']=$uid;
				$point['status']=array('in',array(0,1));
				$point=db('ClassPoint')->where($point)->find();
				if(!empty($point)){
				   $data['point']=$point;
			   }
	   }
	   $data['sta']=1;
	   $data['shop']=$shop;
	   $data['class']=$detail;
	   $data['class_cash']=$class_cash;
	   return json($data);
	}
	//扫码显示信息
	public function class_cash_add(){
		$uid=input('uid');
		$class_id=input('class_id');
		$xuid=input('xuid');
		if(empty($uid)||empty($class_id)||empty($xuid)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$class=db('class')->find($class_id);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$class['shop_id'])->where('is_admin','in',array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$userinfo=db('user')->field('id,nickname,avatar,mobile')->find($xuid);
		$data['sta']=1;
		$data['class']=$class;
		$data['userinfo']=$userinfo;
		return json($data); 
	}
	//充值课程
	public function class_cash_adds(){
		$uid=input('uid');
		$class_id=input('class_id');
		$xuid=input('xuid');
		if(empty($uid)||empty($class_id)||empty($xuid)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$class=db('class')->find($class_id);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('user_id',$uid)->where('shop_id',$class['shop_id'])->where('is_admin','in',array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$acash=db('ClassCash')->where('user_id',$xuid)->where('class_id',$class_id)->where('num','gt',0)->find();
		if(!empty($acash)){
			$data['sta']=0;
			$data['msg']="该用户有该课程未核销";
			return json($data);
		}
		$cash['user_id']=$xuid;
		$cash['class_id']=$class_id;
		$cash['num']=$class['class_num'];
		$cash['class_name']=$class['name'];
		$cash['createtime']=time();
		$cash['updatetime']=time();
		Db::startTrans();
		try{
			$cid=db('ClassCash')->insertGetId($cash);
			$log['user_id']=$xuid;
			$log['class_id']=$class_id;
			$log['admin_id']=$admin['id'];
			$log['class_name']=$class['name'];
			$log['desc']="充值课程+".$class['class_num']."课时";
			$log['createtime']=time();
			$lid=db('ClassCashLog')->insertGetId($log);
			$class['num']=$class['num']+1;
			db('Class')->update($class);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			 
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//我的课程
	public  function my_class(){
		$uid=input('uid');
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		if(empty($uid)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$type=input('type')?input('type'):1;
		if($type==1){
			$list=db('ClassCash')->field("horse_class_cash.*,class.id as class_id,class.images,class.name,class.price,class.class_num,class.num")->join('class',"class.id=horse_class_cash.class_id")->where('horse_class_cash.user_id',$uid)->limit($start,$pagesize)->order('horse_class_cash.createtime',"desc")->select();
		}else{
			$list=db('ClassPoint')->field("horse_class_point.*,class.id as class_id,class.images,class.name,class.price,class.class_num,class.num")->join('class',"class.id=horse_class_point.class_id")->where('horse_class_point.user_id',$uid)->limit($start,$pagesize)->order('horse_class_point.createtime',"desc")->select();
		}
		$data["sta"]=1;
		$data["list"]=$list;
		return json($data);
	}
	//获取时段和教练
	public function get_time(){
		$class_id=input('class_id');
		if(empty($class_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$class=db('class')->find($class_id);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$time=db('ClassTime')->where("shop_id",$class['shop_id'])->select();
		$jl=db('ShopAdmin')->field('horse_shop_admin.*,user.nickname,user.id as uid')->join('user',"user.id=horse_shop_admin.user_id")->where('horse_shop_admin.shop_id',$class['shop_id'])->where("horse_shop_admin.is_admin","in",array(1,3))->select();
		$data['sta']=1;
		$data['time']=$time;
		$data['jl']=$jl;
		return json($data);
	}
	//订阅消息测试
	public function test(){
		$send=new Send();
		$data = [
                'name5'=>['value'=>"chenhaitao"],//订单编号
                'thing28'=> ['value'=>"ceshi"],//订单商品名称
                'time2'=>['value'=>date('Y-m-d h:i:s',time())]//下单时间
            ];
			$page = 'pages/index/index';
			$result = $send->send("oqLAz5E5ISEKlVr0MKQZGcU0n16M",'Iye-QLfWDtLd73Zy08OfiIwCDGBENx6ss2oiA2mWtL0',$data,$page);
	}
	//预约课程
	public function pointment(){
		$uid=input('uid');
		$date=input('date');
		$class_id=input('class_id');
		$time_id=input('time_id');
		$admin_id=input('admin_id');
		if(empty($uid)||empty($class_id)||empty($time_id)||empty($admin_id)){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		if(empty($date)){
			$data['sta']=0;
			$data['msg']="请选择日期";
			return json($data);
		}
		$class=db("class")->find($class_id);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$time=db('ClassTime')->find($time_id);
		if(empty($time)){
			$data['sta']=0;
			$data['msg']="时段有误";
			return json($data);
		}
		$admin=db('ShopAdmin')->find($admin_id);
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="教练信息有误";
			return json($data);
		}
		if($admin["shop_id"]!=$time['shop_id']){
			$data['sta']=0;
			$data['msg']="信息有误";
			return json($data);
		}
		$cash=db('ClassCash')->where('class_id',$class_id)->where('user_id',$uid)->where('num','gt',0)->find();
		if(empty($cash)){
			$data['sta']=0;
			$data['msg']="课时不足";
			return json($data);
		}
		$point['class_id']=$class_id;
		$point['time_id']=$time_id;
		$point['user_id']=$uid;
		$x['status']=array("in",array(0,1));
		$a=db('ClassPoint')->where($point)->where($x)->find();
		$point['admin_id']=$admin_id;
		$point["date"]=date('Y-m-d',ceil($date/1000));
		$point["shop_id"]=$admin['shop_id'];
		if(!empty($a)){
			$data['sta']=0;
			$data['msg']="已预约";
			return json($data);
		}
		$point['createtime']=time();
		Db::startTrans();
		try{
			Db('ClassPoint')->insertGetId($point);
			Db::commit();
			$data['sta']=1;
			$data['msg']="预约成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
		
	}
	//待确认课程
	public function  point_class(){
		$uid=input('uid');
		$shop_id=input('shop_id');
		$status=input('status')?input('status'):0;
		if(empty($shop_id)||empty($uid)){
			$data['sta']=0;
			$data['msg']="已预约";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('shop_id',$shop_id)->where('user_id',$uid)->where('is_admin',"in",array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无权限操作";
			return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize; 
		$list=db('ClassPoint')->field('class.id as class_id,class.images,class.price,class.class_num,class.name,user.nickname,user.id as uid,user.mobile,horse_class_point.*,ClassTime.id as t_id,ClassTime.starttime,ClassTime.endtime')
			->join('ClassTime',"ClassTime.id=horse_class_point.time_id")
			->join('class',"class.id=horse_class_point.class_id")
			->join('user',"user.id=horse_class_point.user_id")
			->where('admin_id',$admin['id'])
			->where('horse_class_point.status',$status)
			->select();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	
	//确认课程
	public function audit_point(){
		$uid=input('uid');
		$point_id=input('point_id');
		$point=db('ClassPoint')->find($point_id);
		if(empty($point)){
			$data['sta']=0;
			$data['msg']="信息有误";
			return json($data);
		}
		if($point['status']!=0){
			$data['sta']=0;
			$data['msg']="预约状态有误";
			return json($data);
		}
		$admin=db('ShopAdmin')->find($point['admin_id']);
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="教练信息有误";
			return json($data);
		}
		if($admin['user_id']!=$uid){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$cash=db('ClassCash')->where('class_id',$point['class_id'])->where('user_id',$point['user_id'])->where('num','gt',0)->find();
		if(empty($cash)){
			$data['sta']=0;
			$data['msg']="该会员课时不足";
			return json($data);
		}
		$point['status']=1;
		$touser=db('user')->field('nickname,openid,id')->find($point['user_id']);
		$shop=db('shop')->field('id,name,addr')->find($point['shop_id']);
		$class=db('class')->find($point['class_id']);
		$time=db('ClassTime')->find($point['time_id']);
		Db::startTrans();
		try{
			db('ClassPoint')->update($point);
			Db::commit();
			$send=new Send();
			$data = [
                'thing28'=> ['value'=>$class['name']],
                'thing6'=> ['value'=>$shop['name']],
				'date7'=> ['value'=>date('Y年m月d日',strtotime($point['date']))." ".$time['starttime']]
            ];
			
			$page = '/pages/course/index?type=2';
			$result = $send->send($touser['openid'],'Iye-QLfWDtLd73Zy08OfiJjV1IQS9avOsjSVIxC8C6w',$data,$page);
			$rdata['sta']=1;
			$rdata['msg']="确认成功";
			return json($rdata);
		}catch(\Exception $e){
			Db::rollback();
			$rdata['sta']=0;
			$rdata['msg']=$e->getMessage();
			return json($rdata);
		}
		
	}
	//扫码展示信息
	public function class_sm(){
		$uid=input('uid');
		$point_id=input('point_id');
		$point=db('ClassPoint')->find($point_id);
		if(empty($point)){
			$data['sta']=0;
			$data['msg']="预约信息有误";
			return json($data);
		}
		$admin=db('ShopAdmin')->find($point['admin_id']);
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="操作权限不足";
			return json($data);
		}
		if($admin['user_id']!=$uid){
			$data['sta']=0;
			$data['msg']="操作权限不足";
			return json($data);
		}
		$class=db('Class')->find($point['class_id']);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$user=db('user')->field('id,nickname,avatar,mobile')->find($point["user_id"]);
		if(empty($user)){
			$data['sta']=0;
			$data['msg']="用户信息有误";
			return json($data);
		}
		$data['sta']=1;
		$data['user']=$user;
		$data['class']=$class;
		 return json($data); 
	}
	//核销课程
	public function destory_class(){
		$uid=input('uid');
		$point_id=input('point_id');
		$point=db('ClassPoint')->find($point_id);
		if(empty($point)){
			$data['sta']=0;
			$data['msg']="预约信息有误";
			return json($data);
		}
		if($point['status']!=1){
			$data['sta']=0;
			$data['msg']="已核销";
			return json($data);
		}
		$admin=db('ShopAdmin')->find($point['admin_id']);
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="操作权限不足";
			return json($data);
		}
		if($admin['user_id']!=$uid){
			$data['sta']=0;
			$data['msg']="操作权限不足";
			return json($data);
		}
		$class=db('Class')->find($point['class_id']);
		if(empty($class)){
			$data['sta']=0;
			$data['msg']="课程信息有误";
			return json($data);
		}
		$cash=db('ClassCash')->where('class_id',$point['class_id'])->where('user_id',$point['user_id'])->where("num","gt",0)->find();
		if(empty($cash)){
			$data['sta']=0;
			$data['msg']="该会员课时不足";
			return json($data);
		}
		
		$point["status"]=2;
		Db::startTrans();
		try{
			db('ClassPoint')->update($point);
			$cash['num']=$cash['num']-1;
			db('ClassCash')->update($cash);
			$log['user_id']=$point['user_id'];
			$log['class_id']=$point['class_id'];
			$log['type']=1;
			$log['admin_id']=$point['admin_id'];
			$log['class_name']=$class['name'];
			$log['desc']=date("y-m-d",time())."核销课程-1";
			$log['createtime']=time();
			db('ClassCashLog')->insertGetId($log);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
			
		}catch(\Exception $e){
			
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//核销历史
	public function destory_history(){
		$uid=input("uid");
		$shop_id=input("shop_id");
		$admin=db('ShopAdmin')->where("shop_id",$shop_id)->where("user_id",$uid)->where('is_admin',"in",array(1,3))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="权限不足";
			return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$list=db('ClassCashLog')->field('horse_class_cash_log.*,user.id as uid,user.nickname,user.mobile')->join('user',"user.id=horse_class_cash_log.user_id")->where('horse_class_cash_log.admin_id',$admin['id'])->where("horse_class_cash_log.type",1)->limit($start,$pagesize)->order('horse_class_cash_log.createtime',"desc")->select();
		$dhx=db('ClassPoint')->where('admin_id',$admin['id'])->where('status',0)->count();
		$yhx=db('ClassPoint')->where('admin_id',$admin['id'])->where('status',1)->count();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			$data['count']=array('dhx'=>$dhx,'yhx'=>$yhx);
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			$data['count']=array('dhx'=>$dhx,'yhx'=>$yhx);
			return json($data);
		}
	}
	//添加门票
	public function ticket_add(){
		$uid=input('uid');
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('user_id',$uid)->where("shop_id",$shop_id)->where('is_admin',"in",array(1,2))->find();

		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$name=input('name');
		if(empty($name)){
			$data['sta']=0;
			$data['msg']="请输入门票名称";
			return json($data);
		}
		$dnum=input('dnum');
		if(empty($dnum)){
			$data['sta']=0;
			$data['msg']="请输入限购数量";
			return json($data);
		}
		$price=input('price');
		if(empty($price)){
			$data['sta']=0;
			$data['msg']="请输入门票价格";
			return json($data);
		}
		$images=input('images');
		if(empty($images)){
			$data['sta']=0;
			$data['msg']="请上传门票图片";
			return json($data);
		}
		$type=input('type');
		if(empty($type)){
			$data['sta']=0;
			$data['msg']="请选择门票类型";
			return json($data);
		}
		$tic['name']=$name;
		$tic['price']=$price;
		$tic['shop_id']=$shop_id;
		$tic['images']=$images;
		$tic['uid']=$uid;
		$tic['dnum']=$dnum;
		$tic['type']=$type;
		$tic['createtime']=time();
		Db::startTrans();
		try{
			db('ticket')->insertGetId($tic);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//门票列表
	public function ticket_list(){
	   $uid=input('uid');
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('user_id',$uid)->where("shop_id",$shop_id)->where('is_admin',"in",array(1,2))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$list=db('ticket')->where('status',1)->where('shop_id',$shop_id)->limit($start,$pagesize)->order('createtime',"desc")->select();
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	//门票下架
	public function ticket_edit(){
		 $uid=input('uid');
		// $status=input('status');
		 $ticket_id=input('ticket_id');
		 if(empty($uid)||empty($ticket_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		 }
		 $ticket=db('ticket')->find($ticket_id);
		 if(empty($ticket)){
			$data['sta']=0;
			$data['msg']="门票信息有误";
			return json($data);
		 }
		$admin=db('ShopAdmin')->where('user_id',$uid)->where("shop_id",$ticket['shop_id'])->where('is_admin',"in",array(1,2))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$ticket['status']=2;
		Db::startTrans();
		try{
			db('ticket')->update($ticket);
			Db::commit();
			$data['sta']=1;
			$data['msg']="操作成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
	}
	//门票详情
	public function ticket_detail(){
		 $uid=input('uid');
		 $ticket_id=input('ticket_id');
		 $ticket=db('ticket')->find($ticket_id);
		 if(empty($ticket)){
			$data['sta']=0;
			$data['msg']="门票信息有误";
			return json($data);
		 }
		 if($ticket['type']==2){
			$vip=db('ShopVip')->where('shop_id',$ticket['shop_id'])->where('user_id',$uid)->where("endtime","gt",time())->find();
			if(empty($vip)){
				$data['sta']=0;
				$data['msg']="无权限查看";
				return json($data);
			}
		 }
		$shop=db('shop')->find($ticket['shop_id']);
		$data['sta']=1;
		$data['ticket']=$ticket;
		$data['shop']=$shop;
		return json($data);
	}
	//商家订单列表
	public function order_list(){
		$uid=input('uid');
		$shop_id=input('shop_id');
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$status=input('status')?input('status'):1;
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$admin=db('ShopAdmin')->where('user_id',$uid)->where("shop_id",$shop_id)->where('is_admin',"in",array(1,2))->find();
		if(empty($admin)){
			$data['sta']=0;
			$data['msg']="无操作权限";
			return json($data);
		}
		$list=db('order')->where('status',$status)->where('shop_id',$shop_id)->limit($start,$pagesize)->order('createtime',"desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['ids']=explode(',',trim($v["ids"],','));
			$list[$k]['json']=json_decode($v['json'],true);
			$list[$k]['count']=0;
			foreach($list[$k]['json'] as $k1=>$v1){
				$list[$k]['count']=$list[$k]['count']+$v1['num'];
			}
			$list[$k]['goods']=db('ticket')->find($list[$k]['json'][0]["id"]);
			$list[$k]['user']=db('user')->field("nickname,mobile")->find($v['user_id']);
		}
		$count=array();
		$count['zs']=db('order')->where('shop_id',$shop_id)->where('type','ticket')->where("status","neq",0)->count();
		$count['ze']=db('order')->where('shop_id',$shop_id)->where('type','ticket')->where("status","neq",0)->sum('js_money');
		$count['wh']=db('order')->where('shop_id',$shop_id)->where('type','ticket')->where("status","eq",1)->count();
		$data['count']=$count;
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
	//删除过期订单
	public function order_del(){
		$list=db('order')->where('status',0)->where('deltime','lt',time())->delete();
	}
	//
	public function js(){
		$list=db('order')->where('status','gt',0)->where('shop_id',"neq",0)->where('js_money',null)->select();
	    foreach($list as $k=>$v){
			$shop=db('shop')->find($v['shop_id']);
			
			if(!empty($shop)){
				$list[$k]["js_money"]=$v['price']*(100-$shop['dian'])/100;
				db('order')->update($list[$k]);
			}
		}
	}
	public function cash(){
		$uid=input('uid');
		$shop_id=input('shop_id');
		if(empty($uid)||empty($shop_id)){
			$data['sta']=0;
			$data['msg']="参数错误";
			return json($data);
		}
		$money=array();
		$money['ze']=db('order')->where('shop_id',$shop_id)->where('type','ticket')->where("status","neq",0)->sum('js_money');
		$money['ztx']=db('CashLog')->where('shop_id',$shop_id)->where('type','-')->sum('price');
		$money['ktx']=$money['ze']-$money['ztx'];
		$page=input('page')?input('page'):1;
		$pagesize=input('pagesize')?input('pagesize'):10;
		$start=($page-1)*$pagesize;
		$list=db('CashLog')->where('shop_id',$shop_id)->order("createtime","desc")->limit($start,$pagesize)->select();
		$data["money"]=$money;
		if(empty($list)){
			$data['sta']=0;
			$data['msg']="";
			return json($data);
		}else{
			$data['sta']=1;
			$data['msg']=$list;
			return json($data);
		}
	}
}
